<template>
  <el-tree
    :load="loadNode"
    lazy
    :props="props"
    @node-click="handleNodeClick"
  ></el-tree>
</template>

<script>
import api from '../../api'
export default {
  name: "ProductTree",
  data() {
    return {
      props: {
        label: "name",
      },
    };
  },
  methods: {
      loadNode(node, resolve) {
          if(node.level === 0) {
              api.selectItemCategoryByParentId().then(res => {
                  if(res.data.code === 200) {
                      return resolve(res.data.result)
                  }
                  return resolve([])
              }).catch(error => {
                  console.log(error);
              })
          }
          if(node.level >= 1) {
              api.selectItemCategoryByParentId({
                  id:node.data.cid
              }).then(res => {
                  if(res.data.code === 200) {
                      return resolve(res.data.result);
                  }
                  return resolve([]);
              }).catch(error => {
                  console.log(error);
              })
          }
      },
    handleNodeClick(data, Node) {
      this.$emit('onTree', data)
    },
  },
};
</script>

<style>
</style>